package edu.scau.web.pos;

import com.alipay.api.AlipayApiException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.core.constants.AjaxResult;
import edu.scau.core.constants.HttpStatus;
import edu.scau.pos.refund.domain.RefundRecord;
import edu.scau.pos.refund.service.AfterSaleService;
import edu.scau.pos.refund.service.RefundRecordService;
import edu.scau.pos.refund.vo.RefundRecordVo;
import edu.scau.pos.vo.CategoryVo;
import edu.scau.pos.vo.RefundOrderVo;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "售后管理",description = "订单售后管理，退货退款")
@RestController
@Slf4j
@RequestMapping("/afterSale")
public class AfterSaleController {
    @Autowired
    private AfterSaleService afterSaleService;
    @Autowired
    private RefundRecordService refundRecordService;

    @Operation(
            summary = "退货接口",
            description = "退货，访问权限:admin、cashier",
            responses = {
                    @ApiResponse(responseCode = "msg"),
                    @ApiResponse(responseCode = "refundAmount",description = "退款金额"),
                    @ApiResponse(responseCode = "refundTime",description = "退款时间"),

            }
    )
    @PostMapping("/refundService")
    public AjaxResult refundService(@RequestBody @Validated RefundOrderVo refundOrderVo) {
       try {
           Map data = afterSaleService.refundService(refundOrderVo);
           return AjaxResult.success(data);
       }catch (AlipayApiException | JsonProcessingException e) {
           log.error(e.getMessage());
           return AjaxResult.error("系统异常");
       }
    }

    @Operation(
            summary = "部分商品退货",
            description = "订单个别商品退货接口，访问权限:admin、cashier",
            responses = {
                    @ApiResponse(responseCode = "msg"),
                    @ApiResponse(responseCode = "refundAmount",description = "退款金额"),
                    @ApiResponse(responseCode = "refundTime",description = "退款时间"),

            }
    )
    @PostMapping("/refund")
    @Hidden
    @PreAuthorize("hasAnyAuthority('admin','cashier')")
    public AjaxResult refund(@Validated  @RequestBody RefundOrderVo refundOrderVo) {
        try {
            Map data=afterSaleService.refund(refundOrderVo,"");
            return AjaxResult.success(data);
        } catch  (JsonProcessingException | AlipayApiException e){
            log.error(e.getMessage());
            return AjaxResult.error();
        }
    }

    @Operation(
            summary = "取消预定订单",
            description = "取消缺货预定的挂单订单，访问权限:admin、cashier",
            responses = {
                    @ApiResponse(responseCode = "msg"),
                    @ApiResponse(responseCode = "refundAmount",description = "退款金额"),
                    @ApiResponse(responseCode = "refundTime",description = "退款时间"),
            }
    )
    @Hidden
    @GetMapping("/cancelBook")
    @PreAuthorize("hasAnyAuthority('admin','cashier')")
    public AjaxResult cancelBook(@RequestParam("orderId") String orderId) {
       try {
           Map data=afterSaleService.cancelBook(orderId,"refund");
           return AjaxResult.success(data);
       }catch (JsonProcessingException | AlipayApiException e){
           log.info(e.getMessage());
           return AjaxResult.error("取消预订失败");
       }
    }

    @Operation(
            summary = "查询退款状态",
            description = "查询退款状态，访问权限:admin、cashier",
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),

            }
    )
    @GetMapping("/queryRefundStatus")
    @PreAuthorize("hasAnyAuthority('admin','cashier')")
    public AjaxResult queryRefundStatus(@RequestParam("refundId") String refundId) {
        try {
            Map data=afterSaleService.queryRefundStatus(refundId);
            return AjaxResult.success(data);
        }catch (AlipayApiException | JsonProcessingException e){
            log.error(e.getMessage());
            return AjaxResult.error("查询异常");
        }
    }

    @Operation(
            summary = "分页获取退货记录",
            description = "获取所有退货记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = RefundRecordVo.class))),
            }
    )
    @GetMapping("/getAllRefundRecord")
    public AjaxResult getAllRefundRecord(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<RefundRecordVo> refundRecords=refundRecordService.getRefundRecordsList();
        PageInfo<CategoryVo> pageInfo = new PageInfo(refundRecords);
        return AjaxResult.success(pageInfo);
    }

    @Operation(
            summary = "重新退款",
            description = "对退款失败的请求进行重新申请退款"
    )
    @GetMapping("/reRefund")
    public AjaxResult reRefund(@RequestParam("refundId") String refundId) {
        try {
            Map data = afterSaleService.reRefund(refundId);
            return AjaxResult.success(data);
        }catch (AlipayApiException | JsonProcessingException e){
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/insert")
    @Hidden
    public AjaxResult insert(@RequestBody RefundRecord refundRecord){
        refundRecordService.addCache(refundRecord);
        return AjaxResult.success();
    }

    @Operation(
            summary = "删除退款记录",
            description = "删除退款记录"
    )
    @Hidden
    @DeleteMapping("/deletedRecord")
    public AjaxResult deletedRecord(@RequestParam("refundId") String refundId){
        refundRecordService.deletedRefundRecord(refundId);
        return AjaxResult.success();
    }
}
